#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    serialPortInfoList = QSerialPortInfo::availablePorts();
    foreach (const QSerialPortInfo &serialPortInfo, serialPortInfoList) {
            qDebug() << serialPortInfo.portName();
    }

    serial = new QSerialPort("COM20");
    if(!serial->open(QIODevice::ReadWrite))
    {
        qDebug() << "open port error!\r\n";
        return;
    }

    serial->setBaudRate(QSerialPort::Baud57600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);

    connect(serial, SIGNAL(readyRead()), this, SLOT(serialRead()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::serialRead()
{
    QByteArray temp = serial->readAll();
    if(!temp.isEmpty())
        qDebug() << temp.toHex() << "len: " << temp.size();
}

void MainWindow::on_calibrationPB_clicked()
{
    //QByteArray str("hello");
    char str[10] = {0xaa, 0x02, 0x00, 0x55};
    qDebug() << "calibration";
    serial->write(str, 4);
}
